//
//  ZHTabBarPlayButton.m
//  XMLYFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZHTabBarPlayButton.h"
#import "ZHPlayViewController.h"
#import "UIButton+WebCache.h"


@implementation ZHTabBarPlayButton


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
    
    }
    
    return self;
}

- (void)createView{
    
    self.backgroundColor = [UIColor clearColor];
    self.frame = CGRectMake(SCREEN_WIDTH_ZH *0.4, -24, SCREEN_WIDTH_ZH *0.2, SCREEN_HEIGHT_ZH *0.2);
    self.userInteractionEnabled = YES;
    
    // 背景
    self.btnBackground = [[BaseImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH*0.2, SCREEN_WIDTH_ZH *0.2)];
    self.btnBackground.backgroundColor = [UIColor clearColor];
    self.btnBackground.image = [UIImage imageNamed:@"tabbar_np_normal@2x.png"];
    self.btnBackground.tag = 1000;
    [self addSubview:self.btnBackground];
    
    // 条
    UIImageView *tiao = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH_ZH*0.2, SCREEN_WIDTH_ZH*0.2)];
    tiao.backgroundColor = [UIColor clearColor];
    tiao.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    tiao.center = CGPointMake(SCREEN_WIDTH_ZH*0.1, SCREEN_WIDTH_ZH*0.1);
    [self.btnBackground addSubview:tiao];
    
    //旋转ImageView
    self.musicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.musicButton.frame = CGRectMake(0, 0, SCREEN_WIDTH_ZH*0.15, SCREEN_WIDTH_ZH*0.15);
    self.musicButton.center = CGPointMake(SCREEN_WIDTH_ZH*0.1, SCREEN_WIDTH_ZH*0.1);
    self.musicButton.backgroundColor = [UIColor clearColor];
    [self.musicButton sd_setImageWithURL:[[[ZHFMDB shareFMDB] selectTrackImageData] lastObject] forState:UIControlStateNormal placeholderImage:PLACEHOLDER_IMAGE];
    
    [self.musicButton setBackgroundImage:PLACEHOLDER_IMAGE forState:UIControlStateNormal];
    self.musicButton.layer.masksToBounds = YES;
    self.musicButton.layer.cornerRadius = SCREEN_WIDTH_ZH*0.075;
    self.musicButton.center = CGPointMake(SCREEN_WIDTH_ZH*0.1, SCREEN_WIDTH_ZH*0.1);
    [tiao addSubview:self.musicButton];
    [tiao bringSubviewToFront:self.playButton];
    
    //播放按钮
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.playButton.backgroundColor = [UIColor clearColor];
    self.playButton.frame = CGRectMake(0, 0, SCREEN_WIDTH_ZH*0.2, SCREEN_WIDTH_ZH*0.2);
    self.playButton.center = CGPointMake(SCREEN_WIDTH_ZH*0.1, SCREEN_WIDTH_ZH*0.1);
//    self.playButton.layer.masksToBounds = YES;
//    self.playButton.layer.cornerRadius = SCREEN_WIDTH_ZH*0.075;
    [self.playButton setImage:[UIImage imageNamed:@"tabbar_np_play@2x.png"] forState:UIControlStateNormal];
    self.playButton.alpha = 0.6;
    [tiao addSubview:self.playButton];
    [self.playButton addTarget:self action:@selector(playMusic:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btnBackground.userInteractionEnabled = YES;
    tiao.userInteractionEnabled = YES;
    


}
/**
 *  播放
 *
 *  @param btn 播放
 */
- (void)playMusic:(UIButton *)btn{
    ZHPlayViewController *player = [ZHPlayViewController sharaMusicPlayer];
//    if ([ZHPlayViewController sharaMusicPlayer].player.state == STKAudioPlayerStatePlaying) {
//        <#statements#>
//    }
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc]initWithRootViewController:player];
    NSMutableArray *array = [[ZHFMDB shareFMDB] selectTrackData] ;
    ZTAlbumModel *albumModel = [array lastObject];
    player.song_id = albumModel.trackId;
    player.allMusicArray = array;
    player.indexRow = array.count - 1;
    NSLog(@"@param btn 播放song_id = %ld",player.song_id);
    [player.player resume];
    [self.window.rootViewController presentViewController:naVC animated:YES completion:^{
        
        [self startCircle];
        
    }];
}
#pragma mark - musicButton旋转动画
- (void)startCircle{
    //改变view Z轴
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //旋转的起始值 从零开始转
    animation.fromValue = [NSNumber numberWithInt:0];
    
    //最终旋转的角度是360°
    animation.toValue = [NSNumber numberWithInt:M_PI * 2];
    
    //旋转时长
    animation.duration = 5;
    
    //重复次数 0 为无限次
    animation.repeatCount = NSIntegerMax;
    
    //旋转结束后是否要逆向返回原位置
    animation.autoreverses = NO;
    
    //是否按照结束位置继续旋转
    animation.cumulative = YES;
    
    //给view的layer层添加动画,key是标记  随便起名字
    [self.musicButton.layer addAnimation:animation forKey:@"circle"];
    self.musicButton.layer.speed = 1.0;
    self.playButton.alpha = 0;
    self.playButton.enabled = NO;
}
// 停止
- (void)stopCircle{
    
    self.musicButton.layer.speed = 0;
    self.playButton.alpha = 1;
    self.playButton.enabled = YES;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
